package com.wisdytech.app.produceExecute.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.wisdytech.linkdcs.account.model.TtEqAccount;
import com.wisdytech.linkdcs.account.service.IAccountRegisterService;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.ieMaintenance.model.IeMaintenance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.app.produceExecute.common.ContentUtil;
import com.wisdytech.app.produceExecute.common.FilterMes;
import com.wisdytech.app.produceExecute.common.StandardData;
import com.wisdytech.app.produceExecute.common.StandardReturnResult;
import com.wisdytech.app.produceExecute.dto.EquipDataSearchDTO;
import com.wisdytech.app.produceExecute.service.EqProduceService;
import com.wisdytech.app.produceExecute.vo.CollectParamVO;
import com.wisdytech.app.produceExecute.vo.EqAccountVO;
import com.wisdytech.app.produceExecute.vo.EquipModelVO;
import com.wisdytech.app.produceExecute.vo.EquipParamVO;
import com.wisdytech.app.produceExecute.vo.EquipRealDataVO;
import com.wisdytech.app.produceExecute.vo.EquipRunStateVO;
import com.wisdytech.linkdcs.equipment.model.CollectionParam;


/**
 * IIOT生产执行-设备对外接口
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/equipment/produce")
public class EqProduceAction {

    @Autowired
    private EqProduceService eqProduceService;

    @Autowired
    private IAccountRegisterService iAccountRegisterService;
    /**
     * 通过产线编码和工序编码获得设备信息
     * @return 设备台账列表信息
     * @throws IOException
     */
    @RequestMapping(value = "/listEquipByLineAndOp/get.mvc")
    @ResponseBody
    public StandardReturnResult<EqAccountVO> listEquipByLineAndOp(@RequestBody String queryMap) throws IOException{
        //%7B%22queryMap%22%3A%7B%22GX001%22%3A%22GX001%22%2C%22equipCodeOrName%22%3A%22%22%2C%22lineCode%22%3A%22CX003%22%7D%7D
        String equipCode = ContentUtil.changeContent(queryMap, "equipCode");
        String lineCode = ContentUtil.changeContent(queryMap, "lineCode");
        String opCode = ContentUtil.changeContent(queryMap, "opCode");
        String shopCode = ContentUtil.changeContent(queryMap, "shopCode");
        String areaCode = ContentUtil.changeContent(queryMap, "areaCode");
        String equipCodeOrName = ContentUtil.changeContent(queryMap, "equipCodeOrName");
        String equipId = ContentUtil.changeContent(queryMap, "equipId");
        String status = ContentUtil.changeContent(queryMap, "status");
        String unStatus = ContentUtil.changeContent(queryMap, "unStatus");
        String realStatus = ContentUtil.changeContent(queryMap, "realStatus");
        String equipType = ContentUtil.changeContent(queryMap, "equipType");
        PageInfo<EqAccountVO> eqPage= eqProduceService.listEquipByLineAndOp(equipCode,lineCode,opCode,shopCode,areaCode,equipCodeOrName,
                equipId,status,unStatus,realStatus,equipType);
        List<EqAccountVO> eqList = eqPage.getList();
        return new StandardReturnResult<EqAccountVO>(eqPage.getTotal(),eqList);
    }

    /**
     * 通过设备编码查询设备实时参数值

     * @return 实时数据列表
     */
    @RequestMapping("/param/listValue/get.mvc")
    @ResponseBody
    public StandardReturnResult<EquipRealDataVO> listHistoryDetail(@RequestBody String queryMap){
        //%7B%22queryMap%22%3A%7B%22collectItemId%22%3A%22C2E16A23327B7A3A95611E44530E3B02%22%2C%22endDate%22%3A1530673218944%2C%22equipCode%22%3A%22SBBH001%22%2C%22startDate%22%3A1530672918944%7D%7D
        EquipDataSearchDTO dto = new EquipDataSearchDTO();
        dto.setCollectItemId(ContentUtil.changeContent(queryMap, "collectItemId"));
        dto.setEndTime(ContentUtil.changeContentDate(queryMap, "endDate").toString());
        dto.setEquipCode(ContentUtil.changeContent(queryMap, "equipCode"));
        dto.setLeftKey(ContentUtil.changeContent(queryMap, "leftKey"));
        dto.setOrder(ContentUtil.changeContent(queryMap, "order"));
        dto.setRightKey(ContentUtil.changeContent(queryMap, "rightKey"));
        //dto.setSize(Integer.parseInt(ContentUtil.changeContent(queryMap, "size")));
        dto.setStartTime(ContentUtil.changeContentDate(queryMap, "startDate").toString());
        //分页查找对象
        PageInfo<EquipRealDataVO> page = eqProduceService.listHistoryDetail(dto);
        List<EquipRealDataVO> list = page.getList();
//    	for (int i=0;i<10;i++){
//            EquipRealDataVO vo = new EquipRealDataVO();
//            vo.setParamValue(""+i);
//            vo.setTime(new Date());
//            list.add(vo);
//        }
        return new StandardReturnResult<EquipRealDataVO>(page.getTotal(),list);
    }

    /**
     * 分页条件查询生产类设备信息
     * @param filter 分页参数
     * @return 生产类设备台账列表信息
     * @throws IOException
     */
    @RequestMapping(value = "/listEqProduceByModelCode/get.mvc")
    @ResponseBody
    public StandardReturnResult<EqAccountVO> listEqProduceByModelCode(@RequestBody String queryMap,FilterMes filter) throws IOException{
        String equipModelCode = ContentUtil.changeContent(queryMap, "equipModelCode");
        String opCode = ContentUtil.changeContent(queryMap, "opCode");
        PageInfo<EqAccountVO> eqPage= eqProduceService.listEqProduceByModelCode(equipModelCode,opCode,filter);
        List<EqAccountVO> eqList = eqPage.getList();
        return new StandardReturnResult<EqAccountVO>(eqPage.getTotal(),eqList);
    }


    /**
     *
     * <p>Description: 通过设备编码查询区域环境参数</p>
     * @return
     */
    @RequestMapping("/param/environment/get.mvc")
    @ResponseBody
    public StandardReturnResult<EquipParamVO> getEquipmentRealParamValue( @RequestBody String queryMap){
    	String equipCode = ContentUtil.changeContent(queryMap, "equipCode");
        //获取设备区域信息
        Equipment equipment = iAccountRegisterService.getEquipAreaByCode(equipCode);
        //获取区域下所有环境类设备
        List<Equipment> listEq = new ArrayList();
        if(equipment!=null){
            listEq = iAccountRegisterService.getAllEquipAreaByAreaId(equipment.getAreaId());
        }
        //封装所有数据
        List<EquipParamVO> voList = new ArrayList<>();
        if (!listEq.isEmpty()){
            for(Equipment t : listEq){
                //获取该设备下所有参数
                List<CollectionParam>  params= eqProduceService.getEuipmentParamView(t.getEquipCode());
                //通过设备编号及参数，获取参数及参数值
                List<EquipParamVO> realData= eqProduceService.listRealData(equipCode, params);
                for(EquipParamVO vo : realData){
                    voList.add(vo);
                }
            }
        }
        PageInfo<EquipParamVO> page = new PageInfo<>(voList);
        return new StandardReturnResult<EquipParamVO>(page.getTotal(),voList);
    }

    /**
     * <p>Description: 通过设备编码查询采集项明细信息</p>
     * @return
     */
    @RequestMapping("/getCollectionParamByEqCode/get.mvc")
    @ResponseBody
    public StandardReturnResult<CollectParamVO> getCollectionParamByEqCode(@RequestBody String queryMap,FilterMes FilterMes){
        String equipCode = ContentUtil.changeContent(queryMap, "equipCode");
        String paramType = ContentUtil.changeContent(queryMap, "paramType");
        //获取采集项明细
        List<CollectParamVO>  params= eqProduceService.getCollectionParamByEqCode(equipCode,paramType,FilterMes);
        PageInfo<CollectParamVO> page = new PageInfo<>(params);
        return new StandardReturnResult<CollectParamVO>(page.getTotal(),params);
    }

    /**
     *
     * <p>Description: 通过工序编码查询设备型号信息</p>

     * @return
     * @throws IOException
     */
    @RequestMapping("/getEquipModelByopCode/get.mvc")
    @ResponseBody
    public StandardReturnResult<EquipModelVO> getEquipModelByopCode(@RequestBody String queryMap,FilterMes FilterMes) throws IOException{
        String opCode = ContentUtil.changeContent(queryMap, "opCode");
        String modelType = ContentUtil.changeContent(queryMap, "modelType");
        List<EquipModelVO>  params= eqProduceService.getEquipModelByopCode(opCode,modelType,FilterMes);
        PageInfo<EquipModelVO> page = new PageInfo<>(params);
        return new StandardReturnResult<EquipModelVO>(page.getTotal(),params);
    }

    /**
     *
     * <p>Description: 通过设备编码查询设备运行情况</p>
     * @return
     */
    @RequestMapping("/getEquipRunStateByEqCode/get.mvc")
    @ResponseBody
    public StandardData getEquipRunStateByEqCode(@RequestBody String queryMap,FilterMes FilterMes){
        String equipCode = ContentUtil.changeContent(queryMap, "equipCode");
//        List<EquipRunStateVO>  params= eqProduceService.getEquipModelByopCode(equipCode,FilterMes);
        EquipRunStateVO vo = new EquipRunStateVO();
        vo.setEquipCode("ABC");
        vo.setEquipName("设备");
        vo.setId("82e7ef12-3d81-4c6f-8e98-c5dd370a3d89");
        vo.setOutPutNum("11");
        vo.setStartDate(new Date());
        vo.setTotalRunTime("20");
        return new StandardData(vo);
    }

    /**
     *
     * <p>Description: 通过设备编码及产品编码查询IE产能</p>
     * @return
     */
    @RequestMapping("/getIeMaintenanceByEqCAndProC/get.mvc")
    @ResponseBody
    public StandardReturnResult<IeMaintenance> getIeMaintenanceByEqCAndProC(@RequestBody String queryMap){
        String equipCode = ContentUtil.changeContent(queryMap, "equipCode");
        String productCode = ContentUtil.changeContent(queryMap, "productCode");
        IeMaintenance ieMaintenance = eqProduceService.getIeMaintenanceByEqCAndProC(equipCode,productCode);
        List<IeMaintenance> list = new ArrayList();
        list.add(ieMaintenance);
        return new StandardReturnResult<IeMaintenance>(1,list);
    }


}
